Ext.ns('ru.proburo.oms.order.dao');

/**
 * ru.proburo.oms.order.dao.OrderLineStore
 * @extends Ext.data.GroupingStore
 */
if(true === Ext.isAir)
{
	ru.proburo.oms.order.dao.OrderLineStore = Ext.extend(Ext.data.GroupingSQLiteStore, {
		constructor: function(config)
		{
			config = config || {};
			
			Ext.applyIf(config, {
				dbFile:    ru.proburo.oms.Constants.DATABASE,
				tableName: 'OrderLine',
				key: 	   'rowid',
				
				fields: ru.proburo.oms.order.entities.OrderLine,
				
				groupField: 'order_id',
				
				autoSave:   false,
				remoteSort: true
			});
			
			ru.proburo.oms.order.dao.OrderLineStore.superclass.constructor.call(this, config);
		},
		
		load: function(options)
		{
			options = options || {};
			//LEFT JOIN `Item` ON `OrderLine`.item_id = `Item`.id 
			this.baseParams.where  = ' WHERE `OrderLine`.order_id = ' + this.order_id + ' ';
			
			ru.proburo.oms.order.dao.OrderLineStore.superclass.load.call(this, options);
		},
		
		setOrderID: function(id)
		{
			this.order_id = id;
		}
	});
}
else
{
	ru.proburo.oms.order.dao.OrderLineStore = Ext.extend(Ext.data.GroupingStore, {
		constructor: function(config)
		{
			config = config || {};
			
			Ext.applyIf(config, {
				url:    '/services/order/line/list.json',
				
				fields: ru.proburo.oms.order.entities.OrderLine,			
				
				groupField: 'order_id',
				
				reader: new Ext.data.JsonReader({
					idProperty:      'id',
					root:            'items',
					fields:          ru.proburo.oms.order.entities.OrderLine,
					successProperty: 'success'
				}),
				writer: new Ext.data.JsonWriter({
					encode:         true,
					listful:        true,
					writeAllFields: true
				}),
				
				autoSave: false,
				
				//TODO: fix default sortInfo
				sortInfo:   {field: 'id', direction: 'ASC'},    
				groupField: 'order_id'
			});
			
			ru.proburo.oms.order.dao.OrderLineStore.superclass.constructor.call(this, config);
		},
		
		setOrderID: function(id)
		{
			this.setBaseParam('order_id', id);
		}
	});
}